import prisma from '@/server/lib/prisma';
import { Prisma } from '@prisma/client';

export default defineEventHandler(async (event) => {
  try {
    const query = getQuery(event);
    const page = Number(query.page || 1);
    const pageSize = Number(query.pageSize || 10);
    // 查询条件
    const where: Prisma.sys_postWhereInput = {};
    if (query.postCode) {
      where.postCode = {
        startsWith: query.postCode as string
      }
    }
    if (query.postName) {
      where.postName = {
        startsWith: query.postName as string
      }
    }
    if (query.status) {
      where.status = query.status as string
    }
    if (query.startTime && query.endTime) {
      where.createTime = {
        gte: new Date(query.startTime + ' 00:00:00').toISOString(),
        lte: new Date(query.endTime + ' 23:59:59').toISOString(),
      }
    }

    const result = await prisma.sys_post.findMany({
      where,
      orderBy: {
        postSort: 'asc'
      },
      skip: (page - 1) * pageSize,
      take: pageSize,
    });
    return {
      code: 200,
      data: result,
    }
  } catch (e) {
    throw e;
  } finally {
    await prisma.$disconnect(); // 关闭连接
  }
})
